package com.thesale.thesaleuser;

import com.thesale.thesaleuser.LoaderFactory.YmlPropertyLoaderFactory;
import liquibase.integration.spring.SpringLiquibase;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnResource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;

@Configuration
@Data
@Slf4j
@ConfigurationProperties(prefix = "user.liquibase")
@PropertySource(value = "classpath:User.yml", factory = YmlPropertyLoaderFactory.class)
public class UserConfiguration {

    private String changeLog;

    private boolean enabled;

    private boolean dropFirst;

    @Bean("userLiquibase")
    @ConditionalOnResource(resources = {"classpath:User.yml"})
    public SpringLiquibase liquibase(DataSource dataSource) throws Exception{
        SpringLiquibase liquibase = new SpringLiquibase();
        //设置数据源
        liquibase.setDataSource(dataSource);
        liquibase.setChangeLog(changeLog);
        liquibase.setShouldRun(enabled);
        liquibase.setDropFirst(dropFirst);
        return liquibase;
    }
}
